const express = require("express");
const router = express.Router();
const db = require("../../sqlServer/dbConnect");

// 日志查询
router.post("/info", (req, res) => {
    const { values, pageConfig } = req.body;
    const { page, pageSize } = pageConfig
    const sql = values === null ?
        'select l.Id,l.doName,l.doInfo,l.doTime,u.name,u.role from log l left join user u on l.doName=u.userName order by l.Id limit ?,?;select count(*) as num from log' :
        `select l.Id,l.doName,l.doInfo,l.doTime,u.name,u.role from log l left join user u on l.doName=u.userName where l.doInfo=? order by l.Id limit ?,?;
        select count(*) as num from log l left join user u on l.doName=u.userName where l.doInfo=? order by l.Id `
    const data = values === null ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] : 
    [values.mode,(Number(page) - 1) * Number(pageSize), Number(pageSize),values.mode]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
        } else {
                res.send({
                    msg: "获取成功",
                    status: 200,
                    data: result[0],
                    total: result[1][0].num
                })
        }
    })
})

// 日志-- 删除
router.post("/delete", (req, res) => {
    const { Id } = req.body
    let sql = "delete from log where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
        }
    })
})

// 日志批量 -- 删除
router.post("/delBatch", (req, res) => {
    const { Id } = req.body
    const data = [...Id]
    data.forEach((item,i) => {
        db.sqlQuery(`delete from log where Id=${item}`, (err) => {
            if (err) {
                console.log(err)
                res.send({
                    msg: "删除失败",
                    status: 203
                })
                return
            } else {
                if(i === data.length-1 ){
                    res.send({
                        msg: "删除成功",
                        status: 200
                    })
                }
            }
        })
    })
})



module.exports = router